﻿using System.Web.Mvc;
using AttributeRouting.Web.Mvc;
using CobraCms.AdminConsole.Web.Models.Content;
using CobraCms.Core.Content;
using CobraCms.Core.Content.Interfaces;
using System;
using CobraCms.AdminConsole.Core;
using CobraCms.AdminConsole.Core.Entities;
namespace CobraCms.AdminConsole.Web.Controllers.Content
{
    public class ContentController : Controller
    {
        private IContentRepository _contentRepository;


        public ContentController(IContentRepository contentRepository)
        {
            _contentRepository = contentRepository;
        }

        [GET("/content/nodes/{nodeId}")]
        public ActionResult EditNode(string nodeId)
        {
            var node = _contentRepository.GetContentNode(nodeId);
            if (node == null)
            {
                throw new Exception("No node found with id " + nodeId);
            }

            var model = new EditNodeModel();
            model.Node = new ContentNode
                             {
                                 Id = node.Id,
                                 Name = node.Name,
                                 ParentId = node.ParentId
                             };
        

            return View(model);
        }

        [POST("/content/nodes/{nodeId}")]
        public ActionResult EditNode(EditNodeModel postedModel)
        {

            if (postedModel == null)
            {
                throw new Exception("Nothing");
            }
            if (postedModel.Node == null)
            {
                throw new Exception("Nothing still");
            }            
            var model = new EditNodeModel();
           
            var node = new ContentNode
            {
                Id = postedModel.Node.Id,
                Name = postedModel.Node.Name,
                ParentId = postedModel.Node.ParentId
            };
            node = _contentRepository.SaveContentNode(node);

            model.Node = node;
            model.Succeeded = true;
            AdminRequestContext.Instance.AddAdminUiTask(new ReloadNodeChildren(model.Node.ParentId)); 
            return View(model);
        }

        [GET("/content/nodes/{nodeId}/create")]
        public ActionResult CreateNode(string nodeId)
        {
            var model = new CreateNodeModel();
            model.ParentNodeId = nodeId;
            return View(model);
        }

        [POST("/content/nodes/{nodeId}/create")]
        public ActionResult CreateNode(CreateNodeModel postedModel)
        {
            var model = new CreateNodeModel();
            model.ParentNodeId = postedModel.ParentNodeId;
            model.Name = postedModel.Name;
            model.Succeeded = true;

            var node = new ContentNode
            {
                Name = postedModel.Name,
                ParentId = postedModel.ParentNodeId                               
            };
            node = _contentRepository.SaveContentNode(node);
            AdminRequestContext.Instance.AddAdminUiTask(new ReloadNodeChildren(model.ParentNodeId)); 
            return RedirectToAction("EditNode", new { nodeId = node.Id });            
        }

    }
}
